package com.example.bbs.service;

import com.example.bbs.bean.*;
import com.example.bbs.conf.FileConf;
import com.example.bbs.dao.*;
import com.example.bbs.http.bean.HttpSendBasic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

@Service
public class AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private FileConf fileConf;
    @Autowired
    private NoteMapper noteMapper;
    @Autowired
    private BbstypeMapper bbstypeMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private BbstagMapper bbstagMapper;

    /**
     * 管理员验证账号密码
     *
     * @param username 用户名
     * @param password 密码
     * @return 管理员
     */
    public Admin login(String username, String password) {
        Admin admin = adminMapper.selectByPrimaryKey(username);
        if (admin != null && password.equals(admin.getAdminpass()))
            return admin;
        return null;
    }

    public String uploadFile(MultipartFile file) throws IOException {
        Date date = new Date();
        String fileName = file.getOriginalFilename();
        File f = new File(fileConf.getFileSave() + "/sys/" + date.getTime() + "/" + fileName);
        f.getParentFile().mkdirs();
        file.transferTo(f);
        return "/file/sys/" + date.getTime() + "/" + fileName;
    }

    public HttpSendBasic editNote(Integer noteId, Integer typeid, String noteName, String note) {
        HttpSendBasic httpSendBasic = new HttpSendBasic();
        Note note1 = noteMapper.selectByPrimaryKey(noteId);
        if (note1 == null) {
            httpSendBasic.setSuccess(0);
            httpSendBasic.setMessage("帖子不存在");
            return httpSendBasic;
        }
        if (typeid != null) {
            if (bbstypeMapper.selectByPrimaryKey(typeid) == null) {
                httpSendBasic.setSuccess(0);
                httpSendBasic.setMessage("板块不存在");
                return httpSendBasic;
            } else
                note1.setTypeid(typeid);
        }
        if (noteName != null)
            note1.setNotename(noteName);
        if (note != null)
            note1.setNote(note);
        note1.setLastedittime(new Date());
        noteMapper.updateByPrimaryKeySelective(note1);
        httpSendBasic.setMessage("ok");
        httpSendBasic.setSuccess(1);
        return httpSendBasic;
    }

    public HttpSendBasic deleteNote(Integer noteId) {
        HttpSendBasic sendBasic = new HttpSendBasic();
        Note note = noteMapper.selectByPrimaryKey(noteId);
        if (note == null) {
            sendBasic.setSuccess(0);
            sendBasic.setMessage("帖子不存在");
            return sendBasic;
        }
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andNoteidEqualTo(noteId);
        if(commentMapper.countByExample(commentExample)>0){
            sendBasic.setSuccess(0);
            sendBasic.setMessage("评论不为空");
            return sendBasic;
        }
        noteMapper.deleteByPrimaryKey(noteId);
        sendBasic.setMessage("ok");
        sendBasic.setSuccess(1);
        return sendBasic;
    }

    public HttpSendBasic editComment(Integer commentId, String text) {
        Comment comment = commentMapper.selectByPrimaryKey(commentId);
        HttpSendBasic httpSendBasic = new HttpSendBasic();
        if (comment == null) {
            httpSendBasic.setSuccess(0);
            httpSendBasic.setMessage("评论不存在");
            return httpSendBasic;
        }
        if (text != null)
            comment.setCommenttext(text);
        commentMapper.updateByPrimaryKeySelective(comment);
        httpSendBasic.setMessage("ok");
        httpSendBasic.setSuccess(1);
        return httpSendBasic;
    }

    public HttpSendBasic deleteComment(Integer commentId) {
        HttpSendBasic sendBasic = new HttpSendBasic();
        Comment note = commentMapper.selectByPrimaryKey(commentId);
        if (note == null) {
            sendBasic.setSuccess(0);
            sendBasic.setMessage("评论不存在");
            return sendBasic;
        }
        commentMapper.deleteByPrimaryKey(commentId);
        sendBasic.setMessage("ok");
        sendBasic.setSuccess(1);
        return sendBasic;
    }

    public HttpSendBasic addTagType(String tagName){
        HttpSendBasic sendBasic = new HttpSendBasic();
        Bbstag bbstag = bbstagMapper.selectByTagName(tagName);
        if(bbstag != null){
            sendBasic.setSuccess(0);
            sendBasic.setMessage("该版块已存在");
            return sendBasic;
        }
        Bbstag tag = new Bbstag();
        tag.setTagname(tagName);
        bbstagMapper.insertSelective(tag);
        sendBasic.setSuccess(1);
        sendBasic.setMessage("OK");
        return sendBasic;
    }

    public HttpSendBasic updateTagType(Integer tagId,String tagName){
        HttpSendBasic sendBasic = new HttpSendBasic();
        Bbstag bbstag = new Bbstag();
        bbstag.setTagid(tagId);
        bbstag.setTagname(tagName);
        int result = bbstagMapper.updateByPrimaryKeySelective(bbstag);
        if(result>=0){
            sendBasic.setSuccess(1);
            sendBasic.setMessage("OK");
        }
        else {
            sendBasic.setMessage("修改失败！");
            sendBasic.setSuccess(0);
        }
        return sendBasic;
    }

    public HttpSendBasic deleteTagType(Integer tagid){
        HttpSendBasic sendBasic = new HttpSendBasic();
        Bbstag bbstag = bbstagMapper.selectByPrimaryKey(tagid);

        if(bbstag==null){
            sendBasic.setSuccess(0);
            sendBasic.setMessage("该板块不存在！");
            return sendBasic;
        }
        bbstagMapper.deleteByPrimaryKey(tagid);
        sendBasic.setSuccess(1);
        sendBasic.setMessage("OK");
        return sendBasic;
    }

    public HttpSendBasic addType(Integer tagid,String typename,String icon){
        HttpSendBasic httpSendBasic = new HttpSendBasic();
        Bbstype bbstype = bbstypeMapper.selectByTypeName(typename);
        if(bbstype!=null){
            httpSendBasic.setSuccess(0);
            httpSendBasic.setMessage("该分类已存在");
            return httpSendBasic;
        }
        Bbstype type = new Bbstype();
        type.setTagid(tagid);
        type.setTypeicon(icon);
        type.setTypename(typename);
        bbstypeMapper.insertSelective(type);
        httpSendBasic.setSuccess(1);
        httpSendBasic.setMessage("OK");
        return httpSendBasic;


    }

    public HttpSendBasic updateType(Integer typeid,Integer tagid,String typename,String icon){
        HttpSendBasic httpSendBasic = new HttpSendBasic();
        Bbstype bbstype = new Bbstype();
        bbstype.setTypeid(typeid);
        bbstype.setTagid(tagid);
        bbstype.setTypename(typename);
        bbstype.setTypeicon(icon);
        int result = bbstypeMapper.updateByPrimaryKeySelective(bbstype);
        if(result>=0){
            httpSendBasic.setSuccess(1);
            httpSendBasic.setMessage("OK");
        }else{
            httpSendBasic.setSuccess(0);
            httpSendBasic.setMessage("修改失败！");
        }
        return httpSendBasic;
    }

    public HttpSendBasic deleteType(Integer typeid){
        HttpSendBasic httpSendBasic = new HttpSendBasic();
        Bbstype bbstype = bbstypeMapper.selectByPrimaryKey(typeid);
        if(bbstype == null){
            httpSendBasic.setSuccess(0);
            httpSendBasic.setMessage("该分类不存在！");
            return httpSendBasic;
        }
        bbstypeMapper.deleteByPrimaryKey(typeid);
        httpSendBasic.setSuccess(1);
        httpSendBasic.setMessage("OK");
        return httpSendBasic;

    }

    public HttpSendBasic setNoteTop(Integer top,Integer noteid){
        HttpSendBasic httpSendBasic = new HttpSendBasic();
        Note note = noteMapper.selectByPrimaryKey(noteid);
        if(note == null){
            httpSendBasic.setSuccess(0);
            httpSendBasic.setMessage("该帖子不存在！");
            return  httpSendBasic;
        }
        noteMapper.setTypeTop(top, noteid);
        httpSendBasic.setSuccess(1);
        httpSendBasic.setMessage("OK");
        return httpSendBasic;
    }

    public HttpSendBasic setTypeGood(Integer isGood,Integer noteid){
        HttpSendBasic httpSendBasic = new HttpSendBasic();
        Note note = noteMapper.selectByPrimaryKey(noteid);
        if(note == null){
            httpSendBasic.setSuccess(0);
            httpSendBasic.setMessage("该帖子不存在！");
            return  httpSendBasic;
        }
        noteMapper.setTypeGood(isGood, noteid);
        httpSendBasic.setSuccess(1);
        httpSendBasic.setMessage("OK");
        return httpSendBasic;
    }

    public HttpSendBasic setTypeTittlecolor(String tittleColor,Integer noteid){
        HttpSendBasic httpSendBasic = new HttpSendBasic();
        Note note = noteMapper.selectByPrimaryKey(noteid);
        if(note == null){
            httpSendBasic.setSuccess(0);
            httpSendBasic.setMessage("该帖子不存在！");
            return  httpSendBasic;
        }
        noteMapper.setTypeTittleColor(tittleColor, noteid);
        httpSendBasic.setSuccess(1);
        httpSendBasic.setMessage("OK");
        return httpSendBasic;
    }

}
